<?php echo $this->element('displayHeader'); ?>
<?php echo $form->create('Product', array('type' => 'file', 'url' => array('action' => 'save'))); ?>
<?php //echo $ajax->form('Product', 'post', array('id' => 'formMain', 'type' => 'file', 'update'=>'content', 'indicator' => 'indicator', 'url'=>array('action'=>'save')));                   ?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
        <?php echo $this->element('toolbar', array('buttons' => array('save', 'clear', 'back'))); ?>
        <?php else : // mode edit ?>
        <?php echo $this->element('toolbar', array('buttons' => array('save', 'add', 'refresh_view',
                        array('delete' => array('model' => 'Product', 'title' => 'part_no')), 'back'))); ?>
        <?php endif; ?>
            </div>
            <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'Product', 'title' => 'part_no')); ?></div>
        </div>

<?php echo $form->input('Product.id'); ?>
<?php echo $form->hidden('mode', array('name' => 'data[mode]', 'value' => $mode)); ?>
                <fieldset>
                    <legend>General Information</legend>
                    <table>
                        <tbody>
                            <tr>
                                <td width="120px"><?php echo $form->label('Product.part_no', 'Part no', array('class' => 'required')) ?></td>
                                <td width="280px">
                                    <span id="divPartNo" <?php echo $mode == 'edit' && $special_part_no == true ? 'style="display: none;"' : '' ?>>
                        <?php echo $form->select('Product.supplier_id', $suppliers) ?> -
                        <?php echo $form->select('Product.category_id', $categories) ?> -
                        <?php echo $form->select('Product.type_id', $itsTypes) ?> -
                        <?php echo $form->text('Product.name', array('size' => '6', 'maxlength' => '6')); ?>
                    </span>
                    <span id="divspecialPartNo" <?php echo $mode == 'add' || $mode == 'edit' && $special_part_no == false ? 'style="display: none;"' : '' ?>>
                        <?php echo $form->text('Product.special_supplier', array('size' => '3', 'maxlength' => '3')); ?> -
                        <?php echo $form->text('Product.special_part_no', array('style' => 'width: 68%')); ?>
                    </span>
                </td>
                <td width="150px"><?php echo $form->label('Product.category_id', 'Category') ?></td>
                <td>
                    <span id="divCategory" <?php echo $mode == 'edit' && $special_part_no == true ? 'style="display: none;"' : '' ?>>
                        <?php if ($mode == 'edit')
                            echo $this->data['Category']['name']; ?>
                    </span>
                    <span id="divSpecialCategory" <?php echo $mode == 'add' || $mode == 'edit' && $special_part_no == false ? 'style="display: none;"' : '' ?>>
                        <?php echo $form->Select('Product.special_category_id', $special_categories, $this->data['Product']['category_id']); ?>
                    </span>
                </td>
            </tr>
            <tr>
                <td><?php echo $form->label('Product.supplier_id', 'Vendor', array('class' => 'required')) ?></td>
                <td>
                    <span id="supplier">
                        <?php if ($mode == 'edit')
                            echo $this->data['Supplier']['name']; ?>
                    </span>
                <td><?php echo $form->label('Product.supplier_part_no', 'Vendor Part no') ?></td>
                <td><?php echo $form->text('Product.supplier_part_no', array('size' => 20)) ?> <?php echo $form->error('Product.supplier_part_no') ?></td>
            </tr>
            <tr>
                <td><?php echo $form->label('Product.brand_id', 'Brand', array('class' => 'required')) ?></td>
                <td>
                    <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('Product.brand_id', null, null, array('style' => 'width: 94%')) ?>
                    <?php else : // mode edit ?>
                    <?php echo $this->data['Brand']['name']; ?>
                    <?php endif; ?>
                            </td>
                            <td><?php echo $form->label('Product.model', 'Model', array('class' => 'required')) ?></td>
                            <td><?php echo $form->text('Product.model', array('size' => 20)) ?> </td>
                        </tr>
                        <tr>
                            <td><?php echo $form->label('Product.title', 'Title', array('class' => 'required')) ?></td>
                            <td colspan="3"><?php echo $form->text('Product.title', array('size' => 90)) ?> </td>
                        </tr>
                        <tr>
                            <td class="top"><?php echo $form->label('Product.text', 'Description', array('class' => 'required')) ?></td>
                            <td colspan="3"><?php echo $form->textarea('Product.text', array('cols' => '90', 'rows' => '5')) ?> </td>
                        </tr>
                    </tbody>
                </table>
            </fieldset>

            <fieldset>
                <legend>Inventory Information</legend>
                <table class="wide top">
                    <tbody>
                        <tr>
                            <td class="top" width="120px">
                    <?php echo $form->label('Product.current_stock', 'Current Stock') ?>
                            </td>
                            <td width="100px">
                    <?php echo isset($this->data['Product']['current_stock']) ? $this->data['Product']['current_stock'] : '0 &nbsp;' ?>
                    <?php if ($mode == 'edit') : ?>
                    <?php
                                    echo $ajax->link($html->image('icon/popup.png', array('title' => 'Index information')),
                                            array('controller' => 'products_serials', 'action' => 'popup_view', $this->data['Product']['id']),
                                            array('update' => 'content', 'position' => 'bottom', 'indicator' => 'indicator'), null, false);
                    ?>
                    <?php endif; ?>
                                </td>
                                <td width="140px"><?php echo $form->label('Product.cost', 'Latest Cost'); ?></td>
                                <td class="text-right" width="60px">
                    <?php if ($mode == 'add') : ?>
                    <?php echo $form->text('Product.cost') ?>
                    <?php else : ?>
                                            <span id="ProductCost"><?php echo number_format($this->data['Product']['cost'], 2); ?></span>
                    <?php
                                            if ($session->read('Auth.User.special_user')) {
                                                echo $ajax->editor("ProductCost", "change_price/{$this->data['Product']['id']}/cost", array('clickToEditText' => 'Click for edit product cost.', 'size' => 10));
                                            }
                    ?>
                    <?php endif; ?>
                                        </td>
                                        <td class="text-left" width="100px">
                                            Baht
                    <?php if ($mode == 'edit') : ?>
                                                &nbsp;
                    <?php
                                                echo $ajax->link($html->image('icon/popup.png', array('title' => 'Cost History')),
                                                        array('controller' => 'products', 'action' => 'popup_view_price', $this->data['Product']['id'], 'cost'),
                                                        array('update' => 'content', 'position' => 'bottom', 'indicator' => 'indicator'), null, false);
                    ?>
                    <?php endif; ?>
                                            </td>
                                            <td width="130px"><?php echo $form->label('Product.last_stock_in_date', 'Last Stock in Date'); ?></td>
                                            <td>
                    <?php if ($mode == 'add') : ?>
                                                    -
                    <?php else : ?>
                    <?php echo $this->data['Product']['last_stock_in_date']; ?>
                    <?php endif; ?>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td><?php echo $form->label('Product.unit_id', 'Unit', array('class' => 'required')) ?></td>
                                                    <td>
                    <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('Product.unit_id', $units) ?>
                    <?php else : ?>
                    <?php echo $this->data['Unit']['title']; ?>
                    <?php endif; ?>
                                                            </td>
                                                            <td>
                    <?php echo $form->label('Product.price', 'Latest Normal Price'); ?>
                                                            </td>
                                                            <td class="text-right">
                    <?php if ($mode == 'add') : ?>
                    <?php echo $form->text('Product.price'); ?>
                    <?php else : ?>
                                                                        <span id="ProductPrice"><?php echo number_format($this->data['Product']['price'], 2); ?></span>
                    <?php
                                                                        if ($session->read('Auth.User.special_user')) {
                                                                            echo $ajax->editor("ProductPrice", "change_price/{$this->data['Product']['id']}/price", array('clickToEditText' => 'Click for edit product price.', 'size' => 10));
                                                                        }
                    ?>
                    <?php endif; ?>
                                                                    </td>
                                                                    <td class="text-left">
                                                                        Baht
                    <?php if ($mode == 'edit') : ?>
                                                                            &nbsp;
                    <?php
                                                                            echo $ajax->link($html->image('icon/popup.png', array('title' => 'Price History')),
                                                                                    array('controller' => 'products', 'action' => 'popup_view_price', $this->data['Product']['id'], 'price'),
                                                                                    array('update' => 'content', 'position' => 'bottom', 'indicator' => 'indicator'), null, false);
                    ?>
                    <?php endif; ?>
                                                                        </td>
                                                                        <td><?php echo $form->label('Product.last_dispatch_date', 'Last Dispatch Date'); ?></td>
                                                                        <td>
                    <?php if ($mode == 'add') : ?>
                                                                                -
                    <?php else : ?>
                    <?php echo $this->data['Product']['last_dispatch_date']; ?>
                    <?php endif; ?>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td colspan="5">&nbsp;</td>
                                                                                <td><?php echo $form->label('Product.last_dispatch_customer_id', 'Last Dispatch to'); ?></td>
                                                                                <td>
                    <?php if ($mode == 'add') : ?>
                                                                                        -
                    <?php else : ?>
                    <?php echo !empty($this->data['LastDispatchCustomer'])? $this->data['LastDispatchCustomer']['cust_no'] . ' ' . $this->data['LastDispatchCustomer']['name']: '-'; ?>
                    <?php endif; ?>
                                                                                        </td>
                                                                                    </tr>
                                                                                </tbody>
                                                                            </table>
                                                                        </fieldset>

                                                                        <fieldset>
                                                                            <legend>Barcode</legend>
                                                                            <table class="wide">
                                                                                <tr>
                                                                                    <td width="120px">&nbsp;</td>
                                                                                    <td>
                <?php if ($mode == 'edit' && $this->data['Product']['has_barcode']) : ?>
                                                                                                <img src="<?php echo "{$this->base}/products/barcode_image/{$this->data['Product']['id']}" ?>" alt="barcode image" /><br />
                <?php echo $form->file('Picture.barcode'); ?>
                <?php echo $this->element('toolbar', array('buttons' => array(array('delete' => array('action' => 'delete_barcode', 'params' => $this->data['Product']['id']))))); ?>
                <?php else : ?>
                <?php echo $form->file('Picture.barcode'); ?>
                <?php endif; ?>
                                                                                                </td>
                                                                                            </tr>
                                                                                        </table>
                                                                                    </fieldset>

                                                                                    <fieldset>
                                                                                        <legend>Picture</legend>
                                                                                        <table class="wide">
                                                                                            <tr>
                                                                                                <td width="120px">
                <?php echo $form->label('Picture.product_1', 'Picture 1'); ?><br />
                                                                                                </td>
                                                                                                <td>
                <?php if ($mode == 'edit' && $this->data['Product']['has_picture_1']) : ?>
                                                                                                        <img src="<?php echo "{$this->base}/products/product_image/{$this->data['Product']['id']}/1" ?>" alt="Picture image 1" /><br />
                <?php echo $form->file('Picture.product_1'); ?>&nbsp;
                <?php echo $this->element('toolbar', array('buttons' => array(array('delete' => array('action' => 'delete_picture', 'params' => $this->data['Product']['id'] . '/' . 1))))); ?>
                <?php else : ?>
                <?php echo $form->file('Picture.product_1'); ?>
                <?php endif; ?>
                                                                                                        </td>
                                                                                                        <td width="120px"><?php echo $form->label('Picture.product_2', 'Picture 2'); ?></td>
                                                                                                        <td>
                <?php if ($mode == 'edit' && $this->data['Product']['has_picture_2']) : ?>
                                                                                                                <img src="<?php echo "{$this->base}/products/product_image/{$this->data['Product']['id']}/2" ?>" alt="Picture image 2" /><br />
                <?php echo $form->file('Picture.product_2'); ?>
                <?php echo $this->element('toolbar', array('buttons' => array(array('delete' => array('action' => 'delete_picture', 'params' => $this->data['Product']['id'] . '/' . 2))))); ?>
                <?php else : ?>
                <?php echo $form->file('Picture.product_2'); ?>
                <?php endif; ?>
                                                                                                                </td>
                                                                                                            </tr>
                                                                                                        </table>
                                                                                                        <div class="text-right caption">Max picture size is 50 KB and best view with 320 x 240 px</div>
                                                                                                    </fieldset>
<?php echo $form->end(); // ปิด form product?>

<?php if ($mode == 'edit') : ?>
                                                                                                                        <div id="custProduct">
    <?php if ($session->check('ItsCustProducts.customer_id')) : ?>
    <?php echo $this->requestAction("/its_cust_products/view/customer_id:{$session->read('ItsCustProducts.customer_id')}/product_id:{$this->data['Product']['id']}", array('return')); ?>
    <?php else : ?>
    <?php echo $this->requestAction("/its_cust_products/view/product_id:{$this->data['Product']['id']}", array('return')); ?>
    <?php endif; ?>
                                                                                                                            </div>
<?php endif; ?>

<script type="text/javascript">
    $j(function() {
        // ใช้เมื่อ Select Supplier จะถึงข้อมูล brand มาอัตโนมัติ
        $("ProductSupplierId").observe("change", function() {
            requestOptions("brands", "ProductSupplierId", "ProductBrandId");
        })

        $("ProductSupplierId").observe("change", function(event) {
            requestJSON('suppliers', $F(this), function(data) {
                $("supplier").update(data.Supplier.name);
                // Special part no?
                if (data.Supplier.special_part_no == true) {
                    $("divPartNo").hide();
                    $("divspecialPartNo").show();
                    $("divCategory").hide();
                    $("divSpecialCategory").show();
                } else {
                    $("divPartNo").show();
                    $("divspecialPartNo").hide();
                    $("divCategory").show();
                    $("divSpecialCategory").hide();
                }
            });
        });

        $("ProductCategoryId").observe("change", function(event) {
            requestJSON('categories', $F(this), function(data) {
                $("category").update(data.Category.name);
            });
        });
    })
</script>